<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 1</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="p1.5.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="1.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P1">Part I. The Language</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#1">Chapter 1. Getting Started</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<h1>1 &ndash; Getting Started</h1>

<p>To keep with the tradition,
our first program in Lua just prints <code>"Hello World"</code>:
<pre>
    print("Hello World")
</pre>
If you are using the stand-alone Lua interpreter,
all you have to do to run your first program is to call the interpreter
(usually named <code>lua</code>) with the name of the text file that contains
your program.
For instance,
if you write the above program in a file <code>hello.lua</code>,
the following command should run it:
<pre>
    prompt> lua hello.lua
</pre>

<p>As a slightly more complex example,
the following program defines a function to compute the
factorial of a given number,
asks the user for a number,
and prints its factorial:
<pre>
    -- defines a factorial function
    function fact (n)
      if n == 0 then
        return 1
      else
        return n * fact(n-1)
      end
    end
    
    print("enter a number:")
    a = io.read("*number")        -- read a number
    print(fact(a))
</pre>

<p>If you are using Lua embedded in an application,
such as CGILua or IUPLua,
you may need to refer to the application manual (or to a "local guru")
to learn how to run your programs.
Nevertheless, Lua is still the same language;
most things that we will see here are valid regardless of how
you are using Lua.
For a start, we recommend that you use the stand-alone interpreter
(that is, the <code>lua</code> executable)
to run your first examples and experiments.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="1.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

